package com.google.demo.Activity;

import java.util.Observable;
import java.util.Observer;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.demo.Config;
import com.google.demo.R;
import com.google.demo.ScanQRCodeApp;
import com.google.demo.Data.Controler.DataHelper;
import com.google.demo.Util.Prefs;
import com.google.demo.Util.Util;
import com.google.demo.interfaces.AlertDialogInterface;
import com.google.demo.model.DataResult;
import com.google.demo.model.InputData;

public class InputDataConfirmActivity extends MainActivity implements Observer {
	private ProgressDialog _waitingDialog;
	private InputDataConfirmActivity _me = this;
	private LayoutInflater mLayoutInflater;
	private ScrollView mInputDataConfirmLayout;
	private TextView mTextViewSalesOfPhonesThisMonth;
	private TextView mTextViewSalesOfSimsThisMonth;
	private TextView mTextViewSalesOfCardsThisMonth;
	private Button mBtnDone;
	private InputData mInputData;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getTextViewHeader().setText(getString(R.string.preview_input_data));
		getmButton_left().setVisibility(View.GONE);
		getmButton_right().setVisibility(View.GONE);
		getLeft_view().setVisibility(View.GONE);
		getRight_view().setVisibility(View.GONE);
		
		Bundle extras = this.getIntent().getExtras();
		mInputData = InputData.getInputDataFromBundle(extras);
		
		mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mInputDataConfirmLayout = (ScrollView) mLayoutInflater.inflate(R.layout.inputdata_confirm, null);
		mTextViewSalesOfPhonesThisMonth = (TextView) mInputDataConfirmLayout.findViewById(R.id.txtv_salesOfPhonesThisMonth);
		mTextViewSalesOfSimsThisMonth = (TextView) mInputDataConfirmLayout.findViewById(R.id.txtv_salesOfSimsThisMonth);
		mTextViewSalesOfCardsThisMonth = (TextView) mInputDataConfirmLayout.findViewById(R.id.txtv_salesOfCardsThisMonth);
		
		mTextViewSalesOfPhonesThisMonth.setText(mInputData.getSalesOfPhonesThisMonth() + "");
		mTextViewSalesOfSimsThisMonth.setText(mInputData.getSalesOfSimsThisMonth() + "");
		mTextViewSalesOfCardsThisMonth.setText(mInputData.getSalesOfCardsThisMonth() + "");
		
		mBtnDone = (Button) mInputDataConfirmLayout.findViewById(R.id.btn_done);
		mBtnDone.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onClickButtonDone();
			}
		});
		
		getFrameLayoutMain().removeAllViews();
		getFrameLayoutMain().addView(mInputDataConfirmLayout);
	}

	private void onClickButtonDone()
	{
		if(Util.isNetworkAvailable())
		{
			Prefs pref = new Prefs(_me);
			String userName = pref.getStringValue("username", Config.USERNAME);
			showProcessDialog();
			DataHelper.getInstance().addObserver(_me);
			DataHelper.getInstance().postQRCode(ScanQRCodeApp.Instance().getDeviceId(), userName, mInputData.getLat(), mInputData.getLon(), mInputData.getQRCode(), mInputData.getSalesOfPhonesThisMonth(), mInputData.getSalesOfSimsThisMonth(), mInputData.getSalesOfCardsThisMonth());
		}
		else
		{
			Util.showError(InputDataConfirmActivity.this, getString(R.string.app_name), getString(R.string.NETWORK_ERROR));
		}
	}
	
	private void showProcessDialog() {
		_waitingDialog = new ProgressDialog(this);
		_waitingDialog.setMessage(getString(R.string.processing));
		_waitingDialog.setCancelable(false);
		_waitingDialog.show();
	}

	private void hideProcessDialog() {
		if (_waitingDialog != null && _waitingDialog.isShowing()) {
			_waitingDialog.dismiss();
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		if (data == null)
		{
			hideProcessDialog();
			return;
		}
		DataResult result = (DataResult) data;
		switch (result.getMethodName()) {
		case postQRCode:
			hideProcessDialog();
			if (result.getErrorCode() == null) {
				if(result.getData().equals("Success")){
					Util.showAlertDialog(InputDataConfirmActivity.this, mCallback, false, true, getString(R.string.app_name), getString(R.string.post_success), getString(R.string.OK), "");
				}else{
					Util.showError(InputDataConfirmActivity.this, getString(R.string.app_name), getString(R.string.post_fail));
				}
			}
			else
			{
				Util.showError(InputDataConfirmActivity.this, getString(R.string.app_name),
						result.getErrorCode().getErrorMessage());
			}
			break;
		}
	}
	
	AlertDialogInterface mCallback = new AlertDialogInterface() {
		
		@Override
		public void onChooseYesCallback() {
			// TODO Auto-generated method stub
			_me.onHistoriesButtonClick(null);
		}
		
		@Override
		public void onChooseNoCallback() {
			// TODO Auto-generated method stub
		}
	};
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		DataHelper.getInstance().deleteObserver(InputDataConfirmActivity.this);
	}
}
